import 'package:client_flutter_door/http/box.dart' as http_box;
import 'package:client_flutter_door/http/door.dart';
import 'package:client_flutter_door/model/door_model.dart';
import 'package:client_flutter_door/widgets/btn.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Box extends StatelessWidget {
  final int index;
  final String czh;
  final bool occupied;
  final bool is400AF;

  const Box(
      {super.key, required this.index, this.czh = '', required this.occupied,required this.is400AF});

  @override
  Widget build(BuildContext context) {
    if (index == 23 || index == 24) {
      return const SizedBox(
        width: 90,
        height: 90,
      );
    }

    String tag = index > 23 ? (index - 2).toString() : index.toString();
    return Btn(
      tag: tag,
      title: is400AF?'S$czh':czh,
      color: occupied ? Colors.green : Colors.red,
      onTap: () async {
        try {
          int num = int.parse(tag);
          int result = await Door.open(num);
          debugPrint('$result');
        } catch (e) {
          debugPrint(e.toString());
        }
      },
      onTagTap: () async {
        http_box.Box.setBox(tag, czh, !occupied,is400AF);
        Provider.of<DoorModel>(context, listen: false)
            .setBoxOccupied(tag, !occupied);
        // await Provider.of<DoorModel>(context, listen: false).getBoxs(); //可用：但是全部刷新
      },
    );
  }
}
